﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace GeneralLibs.Lists
{
    public class BieuMauBaoCao : DoiTuong
    {
        #region string Ma property
        public string Ma
        {
            get
            {
                if (_DataRow["Ma"] is string) return (string)_DataRow["Ma"];
                return "";
            }
            set
            {
                _DataRow["Ma"] = value;
            }
        }
        #endregion

        #region string Ten property
        public string Ten
        {
            get
            {
                if (_DataRow["Ten"] is string) return (string)_DataRow["Ten"];
                return "";
            }
            set
            {
                _DataRow["Ten"] = value;
            }
        }
        #endregion

        public BieuMauBaoCao(DataRow dataRow)
            : base(dataRow)
        {
            //this.TrangThai = true;
        }
    }

    public class DanhMucBieuMauBaoCao : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucBieuMauBaoCao _Instance;
        public static DanhMucBieuMauBaoCao Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucBieuMauBaoCao();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "BieuMauBaoCao";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new BieuMauBaoCao((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public BieuMauBaoCao this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new BieuMauBaoCao(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public BieuMauBaoCao NewDoiTuong()
        {
            return new BieuMauBaoCao(DataTable.NewRow());
        }

        public BieuMauBaoCao GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new BieuMauBaoCao(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public BieuMauBaoCao GetByID(Guid objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucBieuMauBaoCao GetAll()
        {
            return Instance;
        }

        public static DanhMucBieuMauBaoCao Filter(string filterExpression)
        {
            try
            {
                DanhMucBieuMauBaoCao obj = new DanhMucBieuMauBaoCao();
                obj.DataView = new DataView(DanhMucBieuMauBaoCao.Instance.DataTable, filterExpression, "Ten", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static BieuMauBaoCao GetByMa(string ma)
        {
            try
            {
                return DanhMucBieuMauBaoCao.Instance.GetByProperty("Ma", ma);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DataTable GetAllAndSort()
        {
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection(GeneralLibs.LocalSettings.ConnectString);
            SqlCommand cmd = con.CreateCommand();
            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                con.Open();
                cmd.CommandText = "SELECT SUBSTRING(ma,1,3) Sort,* FROM BieuMauBaoCao \n" +
                                    "ORDER BY Sort";
                da.SelectCommand = cmd;
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                con.Close();
            }
        }
    }
}
